package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.vo.AttrRespVO;
import com.atguigu.gulimall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;

@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    /*查询*/
    @Resource
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表查询
     * type=1是规格查询也叫base基础查询,type=0是销售属性
     * type=1时:http://localhost:88/api/product/attr/base/list/0?t=1681300710310&page=1&limit=10&key=
     * type=0时:http://localhost:88/api/product/attr/sale/list/0?t=1681300822412&page=1&limit=10&key=
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type) {
        System.out.println("attrType:" + type);
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, type);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 查询规格(就是属性表)他三级分类的完整路径
     * 比如当我们点击电池容量后面的修改,他会把电池容量这个属性的id给我们发来,就是attrId
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        System.out.println("attrId:" + attrId);
        AttrRespVO respVO = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", respVO);
    }

    /**
     * 获取spu规格 /product/attr/base/listforspu/{spuId}
     * 传递的参数:spuId  商品id
     * 响应的数据:"data": [{
     * "id": 43,
     * "spuId": 11,
     * "attrId": 7,
     * "attrName": "入网型号",
     * "attrValue": "LIO-AL00",
     * "attrSort": null,
     * "quickShow": 1
     * }]
     * 与pms_product_attr_value 表的数据一一对相应
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data", entities);
    }
    /*新增*/

    /**
     * 新增属性
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVO attr) {
        /**
         * 打印的信息有:
         * attr:AttrVO(attrId=null, attrName=电池可拆卸, searchType=0, icon=xxxx, valueSelect=可拆卸;不可拆卸,
         * attrType=1, enable=1, catelogId=225, showDesc=0, attrGroupId=8)
         * 好家伙,真就与AttrVO中的属性一一对应呢
         */
        System.out.println("attr:" + attr);
        attrService.saveAttr(attr);

        return R.ok();
    }
    /*删除*/

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }
    /*修改*/

    /**
     * 修改规格,即属性Attr
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVO attr) {
        System.out.println("AttrVO:" + attr);
        // 打印的是AttrVO:AttrVO(attrId=6, attrName=电池容量, searchType=0, icon=xab, valueSelect=5000mAh(typ), attrType=1, enable=1, catelogId=225, showDesc=0, attrGroupId=8)
        //  attrGroupId=7
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 修改规格参数(规格维护里面修改)  /product/attr/update/{spuId}
     * 请求的参数[{  似乎都是pms_product_attr_value里面的参数
     * "attrId": 7,
     * "attrName": "入网型号",
     * "attrValue": "LIO-AL00",
     * "quickShow": 1
     * }, {
     * "attrId": 14,
     * "attrName": "机身材质工艺",
     * "attrValue": "玻璃",
     * "quickShow": 0
     * }, {
     * "attrId": 16,
     * "attrName": "CPU型号",
     * "attrValue": "HUAWEI Kirin 980",
     * "quickShow": 1
     * }]
     * 响应的数据
     * {
     * "msg": "success",
     * "code": 0
     * }
     */
    @PostMapping("/update/{spuId}")
    //@RequiresPermissions("product:attr:update")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities) {
        productAttrValueService.updateSpuAttr(spuId, entities);
        return R.ok();
    }

}
